package com.feifan.web.controller.oa;

import com.feifan.common.annotation.Log;
import com.feifan.common.core.controller.BaseController;
import com.feifan.common.core.domain.AjaxResult;
import com.feifan.common.core.page.TableDataInfo;
import com.feifan.common.enums.BusinessType;
import com.feifan.common.utils.poi.ExcelUtil;
import com.feifan.oa.domain.FlowCategory;
import com.feifan.oa.domain.FlowCategoryDetail;
import com.feifan.oa.domain.FlowCategoryInfo;
import com.feifan.oa.service.IFlowCategoryDetailService;
import com.feifan.oa.service.IFlowCategoryInfoService;
import com.feifan.oa.service.IFlowCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程类别Controller
 * 
 * @author 非凡
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/oa/category")
public class FlowCategoryController extends BaseController
{
    private String prefix = "oa/flow/category";

    @Autowired
    private IFlowCategoryService flowCategoryService;

    @Autowired
    private IFlowCategoryInfoService flowCategoryInfoService;

    @Autowired
    private IFlowCategoryDetailService flowCategoryDetailService;

    @RequiresPermissions("oa:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询流程类别列表
     */
    @RequiresPermissions("oa:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FlowCategory flowCategory)
    {
        startPage();
        List<FlowCategory> list = flowCategoryService.selectFlowCategoryList(flowCategory);
        return getDataTable(list);
    }

    /**
     * 导出流程类别列表
     */
    @RequiresPermissions("oa:category:export")
    @Log(title = "流程类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FlowCategory flowCategory)
    {
        List<FlowCategory> list = flowCategoryService.selectFlowCategoryList(flowCategory);
        ExcelUtil<FlowCategory> util = new ExcelUtil<FlowCategory>(FlowCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增流程类别
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存流程类别
     */
    @RequiresPermissions("oa:category:add")
    @Log(title = "流程类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FlowCategory flowCategory)
    {
        return toAjax(flowCategoryService.insertFlowCategory(flowCategory));
    }

    /**
     * 修改流程类别
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FlowCategory flowCategory = flowCategoryService.selectFlowCategoryById(id);
        mmap.put("flowCategory", flowCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程类别
     */
    @RequiresPermissions("oa:category:edit")
    @Log(title = "流程类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FlowCategory flowCategory)
    {
        return toAjax(flowCategoryService.updateFlowCategory(flowCategory));
    }

    /**
     * 删除流程类别
     */
    @RequiresPermissions("oa:category:remove")
    @Log(title = "流程类别", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        List<FlowCategoryInfo> flowCategoryInfos = flowCategoryInfoService.selectFlowCategoryInfoByFlowCategory(Long.parseLong(ids));
        if (flowCategoryInfos.size() > 0) {
            return AjaxResult.error("该分类下有流程类别详细，不能删除！");
        }

        List<FlowCategoryDetail> flowCategoryDetails = flowCategoryDetailService.selectFlowCategoryDateilByFlowCategoryId(Long.parseLong(ids));
        if (flowCategoryDetails.size() > 0) {
            return AjaxResult.error("该分类下有具体流程，不能删除！");
        }
        return toAjax(flowCategoryService.deleteFlowCategoryByIds(ids));
    }
}
